/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "S_Vk帧缓存.h"
#include "../Core/结构from成员.h"
#include "../buffer/Vk图像.h"


S_Vk帧缓存::S_Vk帧缓存(const ivec2 size)
	:S_帧缓存(size)
{
	//m_帧缓存;

}

void S_Vk帧缓存::f_构建帧缓存(VkDevice& dev, VkRenderPass& rp) {
	vector<VkImageView> 颜色纹理 = f_取_S_VkImage_纹理视图(m_交换面颜色缓存);

	VkFramebufferCreateInfo 帧缓存创建信息 = {};
	帧缓存创建信息.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
	帧缓存创建信息.renderPass = rp;
	帧缓存创建信息.attachmentCount = 颜色纹理.size();
	帧缓存创建信息.pAttachments = 颜色纹理.data();
	帧缓存创建信息.width = m_Size.x;
	帧缓存创建信息.height = m_Size.y;
	帧缓存创建信息.layers = 1;

	m_设备 = dev;
}

void S_Vk帧缓存::f_构建交换链缓存(VkDevice& dev, VkRenderPass & rp, uint8 附件数量) {
	m_设备 = dev;

	vector<VkImageView> 颜色纹理 = f_取_S_VkImage_纹理视图(m_交换面颜色缓存);
	vector<VkImageView> 深度纹理 = f_取_S_VkImage_纹理视图(m_交换面深度缓存);

	uint32 num = 深度纹理.size();
	m_帧缓存.resize(num);



	VkImageView 图像视图附件[4] = { 颜色纹理[0], 0, 0, 0 };
	for (uint8 i = 0; i < num; ++i) {
		图像视图附件[i + 2] = 深度纹理[i];
	}
	//pResolveAttachments

	VkFramebufferCreateInfo 帧缓存创建信息 = {};
	帧缓存创建信息.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
	帧缓存创建信息.renderPass = rp;
	帧缓存创建信息.attachmentCount = 附件数量;
	帧缓存创建信息.pAttachments = 图像视图附件;
	帧缓存创建信息.width = m_Size.x;
	帧缓存创建信息.height = m_Size.y;
	帧缓存创建信息.layers = 1;
	


	for (uint32 i = 0; i < num; ++i) {
		图像视图附件[1] = 颜色纹理[i+1];
		if (vkCreateFramebuffer(dev, &帧缓存创建信息, NULL, &m_帧缓存[i]) != VK_SUCCESS) {
			throw std::runtime_error("创建帧缓存失败!");
		}
	}
	

}

void S_Vk帧缓存::f_构建交换链缓存(S_设备环境& ctx, uint8 附件数量) {
	m_设备 = (VkDevice)(ctx.m_逻辑设备.设备);
	//VkDevice 逻辑设备 = (VkDevice)(ctx.m_逻辑设备.设备);
	//S_绘图设备配置* 绘图设备配置 = (S_绘图设备配置*)(ctx.m_逻辑设备.pNext);
	S_绘图设备配置& 绘图设备配置 = *((S_绘图设备配置*)(ctx.m_绘图设备配置));


	vector<VkImageView> 颜色纹理 = f_取_S_VkImage_纹理视图(m_交换面颜色缓存);
	vector<VkImageView> 深度纹理 = f_取_S_VkImage_纹理视图(m_交换面深度缓存);

	uint32 num = uint32(深度纹理.size());
	m_帧缓存.resize(num);


	VkImageView 图像视图附件[4] = { 颜色纹理[0], 0, 0, 0 };
	for (uint8 i = 0; i < num; ++i) {
		图像视图附件[i + 2] = 深度纹理[i];
	}
	//pResolveAttachments

	VkFramebufferCreateInfo 帧缓存创建信息 = {};
	帧缓存创建信息.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
	帧缓存创建信息.renderPass = 绘图设备配置.m_渲染通道;
	帧缓存创建信息.attachmentCount = 附件数量;
	帧缓存创建信息.pAttachments = 图像视图附件;
	帧缓存创建信息.width = m_Size.x;
	帧缓存创建信息.height = m_Size.y;
	帧缓存创建信息.layers = 1;


	for (uint32 i = 0; i < num; ++i) {
		图像视图附件[1] = 颜色纹理[i + 1];
		if (vkCreateFramebuffer(m_设备, &帧缓存创建信息, NULL, &m_帧缓存[i]) != VK_SUCCESS) {
			throw std::runtime_error("创建帧缓存失败!");
		}
	}

}

void S_Vk帧缓存::f_release()
//void S_Vk帧缓存::f_release(VkDevice 设备)
{
	//VkDevice 设备 = (VkDevice)(ctx.m_逻辑设备.设备);


	for (auto& e : m_交换面颜色缓存) {
		S_Vk图像管理::f_释放图像(m_设备, e);
	}
	for (auto& e : m_交换面深度缓存) {
		S_Vk图像管理::f_释放图像(m_设备, e);
	}


	for (auto& e : m_帧缓存) vkDestroyFramebuffer(m_设备, e, 0);

	m_交换面颜色缓存.clear();
	m_交换面深度缓存.clear();
	m_帧缓存.clear();
	
}

//, vector<VkImageView> 附件图像视口


